﻿--[[
   @brief    game download gift white list
   @author   harleyxwang
   @version  1.0
   @date     2016-12-23
--]]

gift_white_list = 
{
    -- config url
    cfg_url = LoadStr("QUERY_WHITE_LIST_CFG_URL");
    white_list_call_back = "gift_white_list_call_back";
    gift_cfg_tbl = {};   -- key: game_id, value: -1: 拉取失败,可以再次拉取，0：不在领取礼包， 1：可以领取礼包

    -- 是否在配置表里
    get_gift_white_list_cfg = function(game_id)
        local is_in_white = gift_white_list.gift_cfg_tbl[game_id];
        if is_in_white == nil or is_in_white == -1 then
            gift_white_list.query_gift_white_list(game_id);
            return 0;
        else
            return is_in_white;
        end
    end;

    query_gift_white_list = function(game_id)
        local post_data = {};
        post_data["biz"] = 3;
        post_data["gameid"] = game_id;
        post_data["GameID64"] = tostring(game_id);
        post_data["callback"] = gift_white_list.white_list_call_back;
        local login_cfg_url = gift_white_list.cfg_url;
        local result = base_utility.curl_request_async(login_cfg_url, 
            "gift_white_list.on_query_gift_white_list", false, post_data, true);
    end;

    on_query_gift_white_list = function(result)
        base_utility.log("[gift_white_list].on_query_gift_white_list: ".. result);
        local success, rsp_table = gift_white_list.rsp_string_to_table(result, gift_white_list.white_list_call_back);
        if not success then
            base_utility.log("[gift_white_list] on_query_gift_white_list failer.");
            return;
        end
        -- {"Ret":1, "SvrTime": 1477310316, "Msg":"not", GameID=1} 
        -- 0： 黑名单中的用户, 1： 黑名单中不存在该用户,可以领取礼包 其他： 错误
        if rsp_table ~= nil and rsp_table.GameID ~= nil then
            if rsp_table.Ret == 1 then
                gift_white_list.gift_cfg_tbl[tonumber(rsp_table.GameID)] = 1;
            elseif rsp_table.Ret == 0 then
                gift_white_list.gift_cfg_tbl[tonumber(rsp_table.GameID)] = 0;
            else
                gift_white_list.gift_cfg_tbl[tonumber(rsp_table.GameID)] = -1;  -- 允许再次拉取
            end

            -- 抛出事件通知
            local param_tbl = {
                game_id = tonumber(rsp_table.GameID),
                is_in_white_list = gift_white_list.gift_cfg_tbl[tonumber(rsp_table.GameID)]
            };
            event_center.send_event(frame_bottom_event.GIFT_WHITE_LIST_CFG, 
                base_utility.table_to_json(param_tbl));
        end
    end;

    rsp_string_to_table = function(cfg_rsp, call_back_str)
        -- 返回的数据格式为call_back_str(有用的数据)
        if cfg_rsp == nil or call_back_str == nil then
            return false;
        end;

        local prefix = call_back_str .. "(";
        local tail = ")";
        local outer_len = string.len(prefix .. tail);
        local rsp_len = string.len(cfg_rsp);
        if rsp_len < outer_len then
            return false;
        end;

        local prefix_start, prefix_end = string.find(cfg_rsp, prefix, 1, true); -- just find plain text
        if prefix_start == 1 then
            local tail_reg = "%" .. tail .. "[^%" .. tail .. "]*$";
            local tail_start, tail_end = string.find(cfg_rsp, tail_reg); -- find the last tail in cfg_rsp
            if tail_end == rsp_len then
                -- 说明格式正确
                local real_rsp = string.sub(cfg_rsp, prefix_end + 1, tail_start - 1);
                local real_table = base_utility.json_to_table(real_rsp);
                if real_table ~= nil then
                    return true, real_table;   
                end
            end;
        end;
        return false;
    end;
};